﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Events;
using System.Collections.ObjectModel;
using ModulesInfrastructure.ViewModels;
using System.ComponentModel;
using EventInfrastructure;
using Microsoft.Practices.ServiceLocation;
using ShellInfrastructure;
using ModuleExternalProducersDetail.Views;
using ServerConnection.ShopServiceReference;
using ServerConnection;

namespace ModuleExternalProducersDetail.ViewModels
{
    public class ProducerDetailViewModel : IProducerDetailViewModel, INotifyPropertyChanged
    {
        #region Private members

        private IEventAggregator m_eventAggregator;         
        private ObservableCollection<CategoryView> categoriesOfProducer;
        private ObservableCollection<CategoryView> m_subCategoriesOfProducer;

        private CategoryView m_selectedCategory;
        private CategoryView m_selectedSubCategory;
        private List<ProductView> m_productsOfProducer;

        /// <summary>
        /// id of selected producer
        /// </summary>
        private long selectedProducerId;

        #endregion Private members


        #region Private methods

        /// <summary>
        /// This method is called up when the property is changed
        /// </summary>
        /// <param name="propertyName"></param>
        private void m_OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                //fire event
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// When user choose producer, categories are loaded
        /// </summary>
        /// <param name="producerId"></param>
        private void onProducerSelectEvent(long producerId)
        {
            RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            IRegion region = RegionManager.Regions[RegionNames.RightRegionName];
            region.Activate(ServiceLocator.Current.GetInstance<IProducerDetailView>());


            SelectedProducerId = producerId;


            ///update current category list           
           //List<CategoryView> cats = Service.Instance.GetCategoriesOfProducer("eng", (int)producerId, 0).ToList(); ;
            ///convert list 2 obervable
           // ObservableCollection<CategoryView> catsObserv = new ObservableCollection<CategoryView>(cats);
          
            ///update categories on form
            //LoadCategories = catsObserv;

            LoadProducts = Service.Instance.GetAllProductsOfProducer((int)producerId, "eng").ToList();
            ///get list of products
          
            //this.ShowProduct = new List<ProductFull>();
            //this.ShowProduct.Add(Service.Instance.GetProductFullById(id, "eng"));
            //RaisePropertyChanged(() => this.ShowProduct);
        }

        #endregion Private methods

        [Dependency]
        public IRegionManager RegionManager { get; set; }

        
        #region Constructor

        public ProducerDetailViewModel(IEventAggregator eventAggregator)
        {
            this.m_eventAggregator = eventAggregator;

            m_eventAggregator.GetEvent<ProducerSelectEvent>().Subscribe(onProducerSelectEvent);
        }

        #endregion Constructor

        
        #region Public properties

        /// <summary>
        /// Property changes, when user selects concrete producer and then get categories
        /// </summary>
        public ObservableCollection<CategoryView> LoadCategories
        {
            get
            {
                return categoriesOfProducer;
            }
            set
            {
                categoriesOfProducer = value;
                m_OnPropertyChanged("LoadCategories");
            }
        }


        public ObservableCollection<CategoryView> LoadSubCategories
        {
            get
            {
                return m_subCategoriesOfProducer;
            }
            set
            {
                m_subCategoriesOfProducer = value;
                m_OnPropertyChanged("LoadSubCategories");
            }
        }

        /// <summary>
        /// Products are loaded when subcategory is chosen
        /// </summary>
        public List<ProductView> LoadProducts
        {
            get
            {
                return m_productsOfProducer;
            }
            set
            {
                m_productsOfProducer = value;
                m_OnPropertyChanged("LoadProducts");
            }
        }

        /// <summary>
        /// id of selected producer
        /// property is used when producer is selected in listbox
        /// </summary>
        public long SelectedProducerId
        {
            get
            {
                return selectedProducerId;
            }
            set 
            {
                selectedProducerId = value;
                m_OnPropertyChanged("SelectedProducerId");
            }
        }

        public CategoryView OnSelectedCategory
        {
            get { return m_selectedCategory; }
            set
            {
                m_selectedCategory = value;
                if (m_selectedCategory != null)
                {
                    ///update current category list           
                    List<CategoryView> subCats = Service.Instance.GetCategories("eng", m_selectedCategory.Id).ToList(); 
                    ///convert list 2 obervable
                    ObservableCollection<CategoryView> subCategoriesObserv = new ObservableCollection<CategoryView>(subCats);
                     
                    ///update combobox with subcategories
                    LoadSubCategories = subCategoriesObserv;

                    m_OnPropertyChanged("OnSelectedCategory");
                }
            }
        }

        public CategoryView OnSelectedSubCategory
        {
            get { return m_selectedSubCategory; }
            set
            {
                m_selectedSubCategory = value;
                if (m_selectedSubCategory != null)
                {
                    Dictionary<ProductFilter, object> filter = new Dictionary<ProductFilter, object>();
                    filter.Add(ProductFilter.Non, 0);
                 
                    this.LoadProducts = Service.Instance.GetProductsOnPage(m_selectedSubCategory.Id, "eng", ProductSort.NameAsc, 0, 10, filter).ToList();

                    m_OnPropertyChanged("OnSelectedSubCategory");
                }
            }
        }

        #endregion Public properties

        #region Events

        /// <summary>
        /// PropertyChanged is needed for binding to ui
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion Events
    }
}
